package com.service.component.common;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Slf4j
@RestControllerAdvice
/**
 * @author ：Calvin
 * @date ：Created in 2021/7/16 14:35
 * @description：全局异常捕获类
 * @modified By：
 * @version: 1.0.0$
 */
public class GlobalExceptionHandler {

    @ExceptionHandler(UnauthorizedException.class)
    public CommonResult handlerUnauthorizedException(UnauthorizedException e) {
        e.printStackTrace();
        if(StringUtils.contains(e.getMessage(),"not have role")){
            return CommonResult.commonResult(CommonResult.ResultCode.NOT_ROLES_ERROR);
        }
        return CommonResult.commonResult(CommonResult.ResultCode.NOT_PERMISSIONS_ERROR);
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public CommonResult handlerUnauthenticatedException(UnauthenticatedException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.NOT_PERMISSIONS_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.CHECK_ERROR);
    }

    @ExceptionHandler(SQLException.class)
    public CommonResult handlerSQLException(SQLException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.REQUEST_SQL_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public CommonResult handlerIllegalArgumentException(IllegalArgumentException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.INVALID_PARAMERS_ERROR);
    }

    @ExceptionHandler(NumberFormatException.class)
    public CommonResult handlerNumberFormatException(NumberFormatException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.INVALID_FORMAT_ERROR);
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public CommonResult handlerArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.OUT_OF_RANGE_ERROR);
    }
    @ExceptionHandler(AccountException.class)
    public CommonResult handlerAccountException(AccountException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.DATA_NOT_EXIST);
    }
    @ExceptionHandler(NullPointerException.class)
    public CommonResult handlerNullPointerException(NullPointerException e) {
        e.printStackTrace();
        return CommonResult.commonResult(CommonResult.ResultCode.DATA_NOT_EXIST);
    }

    @ExceptionHandler(Exception.class)
    public CommonResult handlerException(Exception e) {
        e.printStackTrace();
        return CommonResult.commonResult(false, CommonResult.ResultCode.SERVER_ERROR.getCode(), e.getCause().getClass().getSimpleName(),e.getCause());
    }
}
